﻿$.fn.autocomplete = function (params) {

    //Selections
    var currentSelection = -1;
    var currentProposals = [];

    //Default parameters
    params = $.extend({
        hints: [],
        placeholder: '输入单位名称搜索',
        width: 200,
        height: 16,
        showButton: true,
        //buttonText: '搜索',
        onSubmit: function (text) { },
        onBlur: function () { }
    }, params);

    //Build messagess
    this.each(function () {
        //Container
        var searchContainer = $('<div></div>')
            .addClass('autocomplete-container')
            .css('height', params.height * 2);

        //Text input		
        var input = $('<input type="text" class="txt search" autocomplete="off" name="query">')
            .attr('placeholder', params.placeholder)
            .addClass('autocomplete-input')
            .css({
                'height': params.height
            });
        var append = $('<div id="append"></div>')
        if (params.showButton) {
            input.css('border-radius', '3px 0 0 3px');
        }

        //Proposals
        var proposals = $('<div></div>')
            .addClass('proposal-box')
            .css('width', params.width + 18)
            .css('top', input.height() + 20);
        var proposalList = $('<ul id="list"></ul>')
            .addClass('proposal-list');

        proposals.append(proposalList);

        input.bind("change paste keyup", function (e) {
            if (e.which != 13 && e.which != 27
                && e.which != 38 && e.which != 40) {
                currentProposals = [];
                currentSelection = -1;
                proposalList.empty();
                if (input.val() != '') {
                    var word = "^" + input.val() + ".*";
                    proposalList.empty();
                    for (var test in params.hints) {
                        if (params.hints[test].match(word)) {
                            currentProposals.push(params.hints[test]);
                            var element = $('<li></li>')
                                .html(params.hints[test])
                                .addClass('proposal')

                                .click(function () {
                                    //
                                    input.val($(this).html());
                                    proposalList.empty();
                                    params.onSubmit(input.val());

                                })
                                .mouseenter(function () {
                                    $(this).addClass('selected');
                                })
                                .mouseleave(function () {
                                    $(this).removeClass('selected');
                                });
                            proposalList.append(element);
                        }
                    }
                }
            }
        });

        input.blur(function (e) {
            currentSelection = -1;
            //proposalList.empty();
            params.onBlur();
        });

        searchContainer.append(input);
        searchContainer.append(proposals);
        ////切换单位按钮，暂不需要
        if (params.showButton) {
            //var button = $('<div></div>')
            //	.addClass('autocomplete-button')
            //    .attr("id", "autocomplete_id")
            //	.html(params.buttonText)
            //	.css({
            //		'height': params.height + 2,
            //		})

            //$("#demo").append(button);
            //searchContainer.append(button);	
        }

        $(this).append(searchContainer);

        if (params.showButton) {
            //Width fix
            //searchContainer.css('width', params.width + button.width() + 50);
        }
    });

    return this;
};